/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class Tbv_provinciaDAOImpl implements orm.dao.Tbv_provinciaDAO {
	public Tbv_provincia loadTbv_provinciaByORMID(int pr_id_provincia) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return loadTbv_provinciaByORMID(session, pr_id_provincia);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia getTbv_provinciaByORMID(int pr_id_provincia) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return getTbv_provinciaByORMID(session, pr_id_provincia);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByORMID(int pr_id_provincia, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return loadTbv_provinciaByORMID(session, pr_id_provincia, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia getTbv_provinciaByORMID(int pr_id_provincia, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return getTbv_provinciaByORMID(session, pr_id_provincia, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByORMID(PersistentSession session, int pr_id_provincia) throws PersistentException {
		try {
			return (Tbv_provincia) session.load(orm.Tbv_provincia.class, new Integer(pr_id_provincia));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia getTbv_provinciaByORMID(PersistentSession session, int pr_id_provincia) throws PersistentException {
		try {
			return (Tbv_provincia) session.get(orm.Tbv_provincia.class, new Integer(pr_id_provincia));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByORMID(PersistentSession session, int pr_id_provincia, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Tbv_provincia) session.load(orm.Tbv_provincia.class, new Integer(pr_id_provincia), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia getTbv_provinciaByORMID(PersistentSession session, int pr_id_provincia, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Tbv_provincia) session.get(orm.Tbv_provincia.class, new Integer(pr_id_provincia), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia[] listTbv_provinciaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return listTbv_provinciaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia[] listTbv_provinciaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return listTbv_provinciaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia[] listTbv_provinciaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tbv_provincia as Tbv_provincia");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Tbv_provincia[]) list.toArray(new Tbv_provincia[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia[] listTbv_provinciaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tbv_provincia as Tbv_provincia");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Tbv_provincia[]) list.toArray(new Tbv_provincia[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return loadTbv_provinciaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return loadTbv_provinciaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Tbv_provincia[] tbv_provincias = listTbv_provinciaByQuery(session, condition, orderBy);
		if (tbv_provincias != null && tbv_provincias.length > 0)
			return tbv_provincias[0];
		else
			return null;
	}
	
	public Tbv_provincia loadTbv_provinciaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Tbv_provincia[] tbv_provincias = listTbv_provinciaByQuery(session, condition, orderBy, lockMode);
		if (tbv_provincias != null && tbv_provincias.length > 0)
			return tbv_provincias[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateTbv_provinciaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return iterateTbv_provinciaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTbv_provinciaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.Bdvendomaticav04PersistentManager.instance().getSession();
			return iterateTbv_provinciaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTbv_provinciaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tbv_provincia as Tbv_provincia");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTbv_provinciaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tbv_provincia as Tbv_provincia");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia createTbv_provincia() {
		return new orm.Tbv_provincia();
	}
	
	public boolean save(orm.Tbv_provincia tbv_provincia) throws PersistentException {
		try {
			orm.Bdvendomaticav04PersistentManager.instance().saveObject(tbv_provincia);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Tbv_provincia tbv_provincia) throws PersistentException {
		try {
			orm.Bdvendomaticav04PersistentManager.instance().deleteObject(tbv_provincia);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Tbv_provincia tbv_provincia)throws PersistentException {
		try {
			if(tbv_provincia.getTbv_regionre_id_region() != null) {
				tbv_provincia.getTbv_regionre_id_region().tbv_provincia.remove(tbv_provincia);
			}
			
			orm.Tbv_comuna[] lTbv_comunas = tbv_provincia.tbv_comuna.toArray();
			for(int i = 0; i < lTbv_comunas.length; i++) {
				lTbv_comunas[i].setTbv_provinciapr_id_provincia(null);
			}
			return delete(tbv_provincia);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Tbv_provincia tbv_provincia, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(tbv_provincia.getTbv_regionre_id_region() != null) {
				tbv_provincia.getTbv_regionre_id_region().tbv_provincia.remove(tbv_provincia);
			}
			
			orm.Tbv_comuna[] lTbv_comunas = tbv_provincia.tbv_comuna.toArray();
			for(int i = 0; i < lTbv_comunas.length; i++) {
				lTbv_comunas[i].setTbv_provinciapr_id_provincia(null);
			}
			try {
				session.delete(tbv_provincia);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Tbv_provincia tbv_provincia) throws PersistentException {
		try {
			orm.Bdvendomaticav04PersistentManager.instance().getSession().refresh(tbv_provincia);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Tbv_provincia tbv_provincia) throws PersistentException {
		try {
			orm.Bdvendomaticav04PersistentManager.instance().getSession().evict(tbv_provincia);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tbv_provincia loadTbv_provinciaByCriteria(Tbv_provinciaCriteria tbv_provinciaCriteria) {
		Tbv_provincia[] tbv_provincias = listTbv_provinciaByCriteria(tbv_provinciaCriteria);
		if(tbv_provincias == null || tbv_provincias.length == 0) {
			return null;
		}
		return tbv_provincias[0];
	}
	
	public Tbv_provincia[] listTbv_provinciaByCriteria(Tbv_provinciaCriteria tbv_provinciaCriteria) {
		return tbv_provinciaCriteria.listTbv_provincia();
	}
}
